<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱子编号" prop="boxCode">
        <el-input
          v-model="queryParams.boxCode"
          placeholder="请输入箱子编号"
          disabled
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入扫码人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:device:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:device:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-right"
          :disabled="multiple"
          @click="handleCk('two')"
        >批量出库</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="箱子编号" align="center" prop="boxCode" width="180"/>
      <el-table-column label="设备编号" align="center" prop="deviceCode" width="200"/>
      <el-table-column label="扫码人" align="center" prop="createName" width="90"/>
      <el-table-column label="时间" align="center" prop="createTime" width="180" />
      <el-table-column label="出库状态" align="center" prop="ckState" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.ckState == 0" style="color: orange;font-weight: bold;">未出库</span>
          <span @click="showCkInfo(scope.row)" v-if="scope.row.ckState == 1" style="color: #409EFF;font-weight: bold;cursor: pointer;">已出库</span>
        </template>
      </el-table-column>
      <el-table-column label="出库时间" align="center" prop="ckTime" width="100"></el-table-column>
      <el-table-column label="客户名称" align="left" header-align="center" prop="customerName" width="200" show-overflow-tooltip></el-table-column>
      <el-table-column label="客户地址" align="left" header-align="center" prop="customerAddress"  show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="left" header-align="center" width="110" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:device:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:device:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-right"
            @click="handleChuku(scope.row,'one')"
            v-if="scope.row.ckState == 0"
            v-hasPermi="['system:device:edit']"
          >出库</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="箱子编号" prop="boxCode">
          <el-input disabled v-model="form.boxCode" placeholder="请输入箱子编号" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出库信息填写 -->
    <el-dialog title="填写出库信息" :visible.sync="ckInfoOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="form.customerAddress"  placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="联系方式" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户联系方式" />
        </el-form-item>
        <el-form-item label="其他信息" prop="customerInfo">
          <el-input v-model="form.customerInfo" placeholder="请输入其他信息" />
        </el-form-item>
        <el-form-item label="客户备注" prop="customerRemark">
          <el-input v-model="form.customerRemark" type="textarea" :rows="3" placeholder="请输入客户备注" />
        </el-form-item>
      </el-form>  
      <div slot="footer" class="dialog-footer">
        <el-button v-if="saveCk=='one'" type="primary" @click="chuKuApi">确 定</el-button>
        <el-button v-if="saveCk=='two'" type="primary" @click="handleCkApi">确 定</el-button>
        <el-button @click="ckInfoOpen=false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 出库信息查看 -->
    <el-dialog title="查看出库信息" :visible.sync="ckInfoOpenView" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" :readonly="true" />
        </el-form-item>
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="form.customerAddress"  placeholder="请输入客户地址" :readonly="true" />
        </el-form-item>
        <el-form-item label="联系方式" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户联系方式" :readonly="true" />
        </el-form-item>
        <el-form-item label="其他信息" prop="customerInfo">
          <el-input v-model="form.customerInfo" placeholder="请输入其他信息" :readonly="true" />
        </el-form-item>
        <el-form-item label="客户备注" prop="customerRemark">
          <el-input v-model="form.customerRemark" type="textarea" :rows="3" placeholder="请输入客户备注" disabled/>
        </el-form-item>
      </el-form>  
      <div slot="footer" class="dialog-footer">
        <el-button @click="ckInfoOpenView=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice, ckDevice } from "@/api/box/device";

export default {
  name: "Device",
  data() {
    return {
      saveCk:'',
      selectDevices:"",
      ckInfoOpenView: false,
      ckInfoOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备数据表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        boxCode: null,
        createName: null,
        createId: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        customerAddress: null,
        customerName: null,
        customerPhone: null,
        customerInfo: null,
        customerRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
            { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        customerAddress: [
            { required: true, message: '请输入客户地址', trigger: 'blur' }
        ],
        customerPhone: [
            { required: true, message: '请输入客户联系方式', trigger: 'blur' }
        ],
      },
      f_boxCode:''
    };
  },
  created() {
    
  },
  methods: {
    //查看出库详情信息
    showCkInfo(row){
      this.ckInfoOpenView = true;
      getDevice(row.deviceCode).then(response => {
        this.form = response.data;
      });
    },
    //出库按钮点击
    handleChuku(row,type){
      this.saveCk = type
      this.ckInfoOpen = true;
      getDevice(row.deviceCode).then(response => {
        this.form = response.data;
      });
    },
    //出库数据提交接口
    chuKuApi() {
      this.form.ckState = 1
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateDevice(this.form).then(response => {
            this.getList();
            if(response.code == 200){
              this.$modal.msgSuccess(response.msg);
            }else{
              this.$modal.msgError(response.msg);
            }
            this.ckInfoOpen = false;
          });
        }
      });  
    },
    handleCk(type){
      this.saveCk = type
      this.selectDevices = this.ids;
      this.reset();
      this.ckInfoOpen = true;
    },
    handleCkApi(){
      ckDevice(this.selectDevices,this.form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.ckInfoOpen = false;
          this.getList();
      });
    },

    childDevice(boxCode){
      this.deviceList = []
      if(boxCode){
        this.f_boxCode = boxCode;
        this.queryParams.boxCode = this.f_boxCode;
        this.form.boxCode = this.f_boxCode;
        this.handleQuery();
      }
    },
    /** 查询设备数据列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceCode: null,
        boxCode: null,
        createTime: null,
        createName: null,
        createId: null,
        remark: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        customerAddress: null,
        customerName: null,
        customerPhone: null,
        customerInfo: null,
        customerRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.boxCode = this.f_boxCode
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.boxCode = this.f_boxCode;
      this.open = true;
      this.title = "添加设备数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceCode = row.deviceCode || this.ids
      getDevice(deviceCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceCode != null) {
            //updateDevice
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceCodes = row.deviceCode || this.ids;
      this.$modal.confirm('是否确认删除设备数据编号为"' + deviceCodes + '"的数据项？').then(function() {
        return delDevice(deviceCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
